﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Word = Microsoft.Office.Interop.Word;
using System.IO;

public partial class Manage_ExportTrain : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Export_Click(object sender, EventArgs e)
    {
        DateTime st = DateTime.Parse(Started.Text.Trim());
        DateTime ed = DateTime.Parse(Ended.Text.Trim());

        Word.Application app = null;
        Word.Document doc = null;

        object ms = Type.Missing;

        try
        {
            CmsDataContext db = new CmsDataContext();
            var q = from aT in db.Train
                    where aT.Started >= st && aT.Started <= ed
                    orderby aT.Started descending
                    select aT;


            string path = Server.MapPath("~/Temps");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string file = Toolkit.NextUUID + ".doc";
            File.Copy(Server.MapPath(Toolkit.PathOfExportTrains), string.Format("{0}/{1}", path, file));

            app = new Word.Application();
            doc = app.Documents.Open(string.Format("{0}/{1}", path, file));

            //Word.Bookmark bm = doc.Bookmarks["Belong"];
            //bm.Range.Text = Toolkit.CurrentUserBelong;

            //bm = doc.Bookmarks["Time"];
            //bm.Range.Text = string.Format("{0:yyyy年MM月dd日}", DateTime.Now);

            //bm = doc.Bookmarks["Manager"];
            //bm.Range.Text = Toolkit.CurrentUserName;

            Word.Bookmark bm = doc.Bookmarks["During"];
            bm.Range.Text = string.Format("{0:yyyy年MM月dd日}-{1:yyyy年MM月dd日}", st, ed);

            Word.Row row;
            Word.Cell cell;
            Word.Table tbl = doc.Tables[1];

            int iRow = 0;
            foreach (Train t in q)
            {
                iRow++;
                row = tbl.Rows.Add();

                cell = row.Cells[1];
                cell.Range.Text = Convert.ToString(iRow);

                cell = row.Cells[2];
                cell.Range.Text = t.Name;

                cell = row.Cells[3];
                cell.Range.Text = string.Format("{0:yyyy-MM-dd HH:mm}", t.Started);

                cell = row.Cells[4];
                cell.Range.Text = t.Address;

                cell = row.Cells[5];
                cell.Range.Text = t.Memo;

                cell = row.Cells[6];
                cell.Range.Text = string.Format("{0}{1}", t.Teacher.Name, t.Teacher.Belong);

                cell = row.Cells[7];
                cell.Range.Text = t.Required;

                cell = row.Cells[8];
                cell.Range.Text = Convert.ToString(t.Amount);
            }

            doc.Save();
            doc.Close(ref ms, ref ms, ref ms);

            doc = null;

            Response.Redirect(string.Format("~/Temps/{0}", file));
        }
        catch (Exception ee)
        {
        }
        finally
        {
            if (app != null)
            {
                app.Quit(ref ms, ref ms, ref ms);
                app = null;
            }
        }
    }
}